/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package espe.ccsoft.dao;

import espe.ccsoft.models.AccionRol;
import espe.ccsoft.orm.ORMBase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class AccionRolDao extends ORMBase<AccionRol> {

    public final void insert(AccionRol accionrol) throws Exception {
        super.insertSql = "INSERT INTO ROL_ACCION ("
                + "ROL_ID,"
                + "ACC_ID"
                + ") VALUES(?, ?)";
        super.insert(new Object[]{
            accionrol.getAccionrol().getId_rol(),
            accionrol.getAccionrol().getId_accion(),});
    }
    
    public final List<AccionRol> findallByRolIdAccionId(final int id_rol, final int id_accion) throws Exception{
        super.findSomeSql = "SELECT RACC.ACC_ID, RACC.ROL_ID "
                + "FROM ROL_ACCION AS RACC "
                + "WHERE RACC.ROL_ID=? AND RACC.ACC_ID=?";

        return super.findSome(new Object[]{
            id_rol,
            id_accion,});
    }
    
    public final List<AccionRol> findAllByPermisoRol(int id_rol, int id_permiso) throws Exception {
        super.findSomeSql = "SELECT RACC.ACC_ID, RACC.ROL_ID "
                + "FROM ACCION AS ACC, ROL AS R, ROL_ACCION AS RACC, "
                + "PERMISO AS P, PERMISOS_ROL PROL "
                + "WHERE ACC.PERM_ID=P.PERM_ID "
                + "AND PROL.PERM_ID=P.PERM_ID "
                + "AND PROL.ROL_ID=R.ROL_ID "
                + "AND RACC.ROL_ID=R.ROL_ID "
                + "AND RACC.ACC_ID=ACC.ACC_ID "
                + "AND R.ROL_ID=? AND P.PERM_ID=?";

        return super.findSome(new Object[]{
            id_rol,
            id_permiso,});
    }
    
    public final AccionRol findAByIdRolIdAccion(int id_rol, int id_accion) throws Exception {
        super.findByPkSql = "SELECT * "
                + "FROM ROL_ACCION AS RACC "
                + "WHERE RACC.ROL_ID=? "
                + "AND RACC.ACC_ID=?";

        return super.findByPK(new Object[]{
            id_rol,
            id_accion,});
    }

    public final void delete(int id_rol) throws Exception {
        super.deleteSql = "DELETE FROM ROL_ACCION WHERE ROL_ID=?";

        super.delete(new Object[]{
            id_rol,});
    }

    @Override
    protected AccionRol crearObjeto(final ResultSet rs) throws SQLException {
        AccionRol accionrol = new AccionRol();
        accionrol.setAccionrol(rs.getInt("ACC_ID"), rs.getInt("ROL_ID"));
        return accionrol;
    }
}
